<?php
class Pages_PageController extends Zend_Controller_Action {
	
	/**
	* список страниц
	*/
	public function indexAction() {
		
		$this->view->headTitle($this->view->translate('Pages'));
		
		$pages = Doctrine_Core::getTable('Pages_Model_Page')->findAll();
		
		$this->view->pages = $pages;
	}
	
	/**
	* Создание страницы
	*/
	public function createAction() {		
		$form = new Pages_Form_Page();
		$form->setAction($this->view->url(array(), 'pages_page_create'));
		$alias = $form->getElement('alias');
		$alias->addValidator(new ZFEngine_Validate_Doctrine_NoRecordExist('Pages_Model_Page','alias'),true);
		if($this->_request->isPost() && $form->isValid($this->_request->getPost())) {
			$values = $form->getValues();
			
			$page = new Pages_Model_Page();
			$page->published = $values['published'];
			$page->show_content = $values['show_content'];
			$page->alias = $values['alias'];			
			
			foreach ($values['name'] as $lang=>$value) {
				$page->Translation[$lang]->name = $value;
			}
			foreach ($values['description'] as $lang=>$value) {
				$page->Translation[$lang]->description = $value;
			}
			foreach ($values['keywords'] as $lang=>$value) {
				$page->Translation[$lang]->keywords = $value;
			}
			$page->save();
			$this->_helper->getHelper('FlashMessenger')
					->addMessage($this->view->translate('Page created success!'));
			return $this->_redirect($this->view->url(array(), 'pages_page_index'));
		}
		$this->view->headTitle($this->view->translate('Create a new page'));
		$this->view->form = $form;
	}
	
	/**
	* Обновление страницы
	*/
	public function updateAction() {
		$id = $this->_request->getParam('id', 0);
		
		$page = Doctrine_Core::getTable('Pages_Model_Page')->find($id);
		
		if(!$id || !$page) {
			throw new Zend_Controller_Action_Exception('Page not found', 404);
		}
		
		$form = new Pages_Form_Page();
		$form->setAction($this->view->url(array('id' => $id), 'pages_page_update'));
		
		
		
		///добавляем иерархию страниц
		$locales = Zend_Controller_Front::getInstance()->getParam('bootstrap')->getOption('locales');
				
		$populate = array(
			'alias' => $page->alias,
			'published' => $page->published,
			'show_content' => $page->show_content
		);
		
		$locales = Zend_Controller_Front::getInstance()->getParam('bootstrap')->getOption('locales');
		
		foreach ($locales as $lang=>$value) {
			$populate['name'][$lang] = $page->Translation[$lang]->name;
			$populate['description'][$lang] = $page->Translation[$lang]->description;
			$populate['keywords'][$lang] = $page->Translation[$lang]->keywords;
		}
		
		$this->view->headTitle($this->view->translate('Update page'));
		$this->view->form = $form;
		
		if($this->_request->isPost() && $form->isValid($this->_request->getPost())) {
			$values = $form->getValues();
			
			$page->published = $values['published'];
			$page->show_content = $values['show_content'];
			$page->alias = $values['alias'];			
			
			foreach ($values['name'] as $lang=>$value) {
				$page->Translation[$lang]->name = $value;
			}
			foreach ($values['description'] as $lang=>$value) {
				$page->Translation[$lang]->description = $value;
			}
			foreach ($values['keywords'] as $lang=>$value) {
				$page->Translation[$lang]->keywords = $value;
			}
			$page->updated_at = time();
			$page->save();
		
			$this->_helper->getHelper('FlashMessenger')
					->addMessage($this->view->translate('Update page success!'));
			return $this->_redirect($this->view->url(array(), 'pages_page_index'));
		} else {
			$form->populate($populate);
		}
	}
	
	public function deleteAction() {
		$id = $this->_request->getParam('id', 0);
		$pageItem = Doctrine_Core::getTable('Pages_Model_Page')->find($id);
		
		if(!$id || !$pageItem) {
			throw new Zend_Controller_Action_Exception('Page not found', 404);
		}
		
		$pageItem->delete();
		
		$this->_helper->getHelper('FlashMessenger')
					->addMessage($this->view->translate('Page has been delete successfully!'));
		return $this->_redirect($this->view->url(array(), 'pages_page_index'));
	}
	/**
	 * 
	 * Просмотр страницы
	 * @throws Zend_Controller_Action_Exception
	 */
	public function showAction() {
		$id = $this->_request->getParam('id', 0);
		
		$page = Doctrine_Core::getTable('Pages_Model_Page')->find($id);
		
		if(!$id || !$page) {
			throw new Zend_Controller_Action_Exception('Page not found', 404);
		}
		
		$nodes = Doctrine_Query::create()
				 ->from('Pages_Model_Node')
				 ->where('page_id = ?', $page->id)
				 ->orderBy('ordering ASC')
				 ->execute();
		$this->view->nodes = $nodes;
		
		$this->view->page = $page;
	}
}